/*
 * Copyright (c) 2010-2011 Plugger Antonio Begue Ponce. All Rights Reserved.
 *
 * This file is part of Plugger Framework.
 *
 *  Plugger Framework is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License version 3 as published by
 *  the Free Software Foundation.
 *
 *  Plugger Framework is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Plugger Framework.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.plugger.infrastructure.domainbase;

import java.io.Serializable;

/**
 * Singleton helper class that manipulate key objects of KeyType.
 *
 * @author Antonio Begue
 * @version $Revision: 1.0 $
 *
 * @see     KeyType
 */
public class KeyObject {
    private static KeyObject instance;
    private KeyType keyType = KeyType.INTEGER;
    private Object newValue = null;

    private KeyObject() {}
    
    /**
     * A static constructor that creates the singleton KeyObject.
     */
    static {
        instance = new KeyObject();
    }

    /**
     * Always return the same instance of KeyObject.
     *
     * @return the static instance of KeyObject.
     */
    public static KeyObject getInstance() {
        return instance;
    }

    /**
     * A new key represent a model never persisted.
     *
     * @return a new key object.
     */
    public Object newKey() {
        return getNewValue();
    }

    /**
     * Help check if a key is in its new state.
     *
     * @param key  the key object to be checked.
     * @return true if the key is in its new state.
     */
    public boolean isNew(Object key) {
        if (newValue == key) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * This function serialize the key as a persistent form.
     *
     * @param key the key object to serialize.
     * @return a serialized key.
     */
    public Serializable getSerialKey(Object key) {
        if (keyType == KeyType.INTEGER) {
            return new Integer(key.toString()).intValue();
        } else if (keyType == KeyType.LONG) {
            return new Long(key.toString()).longValue();
        }
        return null;
    }

    /**
     * Get the specified keyType
     * the default keyType is KeyType.INTEGER.
     *
     * @return the keyType
     */
    public KeyType getKeyType() {
        return keyType;
    }

    /**
     * specify the keyType to be used in the application.
     *
     * @param keyType the keyType to set
     */
    public void setKeyType(KeyType keyType) {
        this.keyType = keyType;

        if (newValue != null) {
            if (keyType == KeyType.INTEGER) {
                setNewValue(new Integer(newValue.toString()).intValue());
            } else if (keyType == KeyType.LONG) {
                setNewValue(new Long(newValue.toString()).longValue());
            }
        }
    }

    /**
     * Get the specified newValue.
     * the newValue is used for a model that is not yet persisted.
     *
     * @return the newValue
     */
    public Object getNewValue() {
        return newValue;
    }

    /**
     *
     * @param newValue the newValue to set
     */
    public void setNewValue(Object newValue) {
        this.newValue = newValue;
    }

    /**
     *
     * @param newValue the newValue to set
     */
    public void setNewValue(int newValue) {
        this.newValue = newValue;
    }

    /**
     *
     * @param newValue the newValue to set
     */
    public void setNewValue(long newValue) {
        this.newValue = newValue;
    }
}
